<template>
	<view class="container">
		<view class="app-info-bar">
			<view class="flex-row items-center space-x-3">
				<image class="app-logo" src="/static/logo.png" mode=""></image>
				<view>
					<text class="app-name">Microsoft Office 2021</text>
					<text class="app-specs">3.2GB • 免费下载</text>
				</view>
			</view>
		</view>
		<scroll-view scroll-y class="scroll-content">
			<view class="section-container">
				<text class="section-title">选择下载方式</text>
				<view class="space-y-3">
					<view class="download-option">
						<view class="flex-row items-center space-x-3">
							<view class="option-icon-box" style="background-color: #F4EADF;">
								<image src="/static/lanzou.png" mode="aspectFit" style="width: 40rpx;height: 40rpx;" />
							</view>
							<view>
								<text class="option-name">迅雷下载</text>
								<text class="option-desc">直接下载</text>
							</view>
						</view>
						<view class="select-empty-circle"></view>
					</view>
					<view class="download-option selected">

						<view class="flex-row items-center space-x-3">
							<view class="option-icon-box bg-blue">
								<text class="iconfont icon-baiduyunpan" style="color: #06A7FF;"></text>
							</view>
							<view>
								<text class="option-name">百度网盘</text>
								<text class="option-desc">高速下载 • 推荐</text>
							</view>
						</view>
						<view class="select-check-circle bg-blue">
						</view>
					</view>
					<view class="download-option">
						<view class="flex-row items-center space-x-3">
							<view class="option-icon-box bg-orange">
								<text class="iconfont icon-aliyunpan" style="color: #6666FF;"></text>
							</view>
							<view>
								<text class="option-name">阿里云盘</text>
								<text class="option-desc">稳定可靠</text>
							</view>
						</view>
						<view class="select-empty-circle"></view>
					</view>
					<view class="download-option">
						<view class="flex-row items-center space-x-3">
							<view class="option-icon-box bg-orange">
								<text class="iconfont icon-kuake" style="color: #3A25DD;"></text>
							</view>
							<view>
								<text class="option-name">夸克云盘</text>
								<text class="option-desc">稳定可靠</text>
							</view>
						</view>
						<view class="select-empty-circle"></view>
					</view>
					<view class="download-option">
						<view class="flex-row items-center space-x-3">
							<view class="option-icon-box bg-green">
								<text class="iconfont icon-xunlei" style="color: #1390F2;"></text>
							</view>
							<view>
								<text class="option-name">迅雷下载</text>
								<text class="option-desc">直接下载</text>
							</view>
						</view>
						<view class="select-empty-circle"></view>
					</view>
				</view>
			</view>
			<view class="section-container" v-if="unlockState !== 'unlocked'">
				<view class="ad-box">
					<text class="ad-title">观看广告获取下载链接</text>
					<text class="ad-subtitle">观看完整广告后即可获得免费下载链接</text>
					<view :class="['ad-player-area', { 'ad-playing': unlockState === 'playing' }]" @click="startAd">
						<view class="ad-player-content">
							<text
								:class="['fas', 'fa-play-circle', 'ad-play-icon', { 'pulse': unlockState !== 'playing' }]"></text>
							<text class="ad-play-text">{{ unlockState === 'playing' ? '广告播放中...' : '点击播放广告' }}</text>
						</view>
						<view class="countdown-wrapper">
							<view class="countdown-svg-box">
								<svg viewBox="0 0 48 48" class="countdown-svg">
									<circle cx="24" cy="24" r="20" stroke="rgba(255,255,255,0.3)" stroke-width="4"
										fill="none" />
									<circle cx="24" cy="24" r="20" stroke="#ffffff" stroke-width="4" fill="none"
										:style="countdownStyle" class="countdown-circle" />
								</svg>
								<view class="countdown-text-box">
									<text class="countdown-text">{{ countdown > 0 ? countdown : '' }}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="ad-meta-info">
						<view class="flex-row items-center space-x-1">
							<text class="fas fa-clock"></text>
							<text>15秒</text>
						</view>
						<view class="flex-row items-center space-x-1">
							<text class="fas fa-volume-up"></text>
							<text>有声广告</text>
						</view>
					</view>
					<button class="unlock-btn" :disabled="unlockState !== 'unlocked'" @click="startAd">
						<text class="fas fa-lock btn-icon"></text>
						<text>观看广告后解锁</text>
					</button>
				</view>
			</view>
			<view class="section-container" v-else>
				<view class="download-links-box">
					<view class="success-message">
						<text class="fas fa-check-circle success-icon"></text>
						<text class="success-title">获取成功！</text>
						<text class="success-subtitle">您的下载链接已准备就绪</text>
					</view>
					<view class="link-item">
						<view class="link-header">
							<text class="link-label">百度网盘链接</text>
							<text class="link-copy-btn" @click="copyLink(downloadData.link)">复制</text>
						</view>
						<text class="link-content">{{ downloadData.link }}</text>
					</view>
					<view class="link-item">
						<view class="link-header">
							<text class="link-label">提取码</text>
							<text class="link-copy-btn" @click="copyLink(downloadData.code)">复制</text>
						</view>
						<text class="code-content">{{ downloadData.code }}</text>
					</view>
					<button class="open-link-btn" @click="openLink(downloadData.link)">
						<text class="fas fa-external-link-alt btn-icon"></text>
						<text>打开百度网盘</text>
					</button>
				</view>
			</view>
			<view class="section-container">
				<view class="instruction-box">
					<view class="flex-row items-start space-x-3">
						<text class="fas fa-info-circle instruction-icon"></text>
						<view>
							<text class="instruction-title">下载说明</text>
							<view class="instruction-list">
								<text class="instruction-list-item">• 观看完整广告后即可获得下载链接</text>
								<text class="instruction-list-item">• 网盘链接包含提取码，请妥善保存</text>
								<text class="instruction-list-item">• 建议使用官方客户端下载以获得最佳速度</text>
								<text class="instruction-list-item">• 如遇问题请联系客服获得帮助</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="bottom-security-tip">
				<view class="bg-gray-tip">
					<text class="fas fa-shield-alt security-icon"></text>
					<text class="security-text">所有软件均经过安全检测，请放心下载使用</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed,
		onUnmounted
	} from 'vue';

	// --- 状态数据 ---
	// 'locked': 初始状态，未观看广告
	// 'playing': 广告播放中
	// 'unlocked': 广告播放完毕，已解锁
	const unlockState = ref('unlocked');
	const countdown = ref(15);
	let countdownTimer = null;

	const downloadData = ref({
		link: 'https://pan.baidu.com/s/1abc123def456',
		code: 'AB8C',
	});

	// --- 计算属性 ---
	const countdownStyle = computed(() => {
		const radius = 20;
		// 周长 C = 2 * pi * r ≈ 125.6
		// 因为 SVG viewBox 是 48x48，r=20，所以周长约 125.66
		// HTML CSS 中的 stroke-dasharray 是 283，我们按 2 * pi * 20 实际周长来计算
		const circumference = 2 * Math.PI * radius;
		// 倒计时动画，从 circumference 到 0
		const offset = circumference * (1 - countdown.value / 15);

		return {
			'stroke-dasharray': circumference,
			'stroke-dashoffset': circumference,
			'animation-duration': '15s' // CSS 动画时长
		};
	});


	const startCountdown = () => {
		clearInterval(countdownTimer);
		countdown.value = 15;

		countdownTimer = setInterval(() => {
			if (countdown.value > 0) {
				countdown.value -= 1;
			} else {
				clearInterval(countdownTimer);
				unlockState.value = 'unlocked';
				uni.showToast({
					title: '解锁成功！',
					icon: 'success'
				});
				console.log('解锁成功');
			}
		}, 1000);
	};

	const startAd = () => {
		if (unlockState.value === 'locked') {
			unlockState.value = 'playing';
			uni.showToast({
				title: '广告开始播放...',
				icon: 'none'
			});
			startCountdown();
		}
	};

	const copyLink = (text) => {
		uni.setClipboardData({
			data: text,
			success: () => {
				uni.showToast({
					title: '复制成功',
					icon: 'none'
				});
			},
			fail: () => {
				uni.showToast({
					title: '复制失败',
					icon: 'error'
				});
			}
		});
	};

	const openLink = (url) => {
		// 外部链接跳转，通常在 UniApp 中需要使用 web-view 或调用特定API
		// 这里仅作控制台输出和提示
		uni.showModal({
			title: '提示',
			content: `即将打开外部链接：${url}`,
			confirmText: '确定',
			success: (res) => {
				if (res.confirm) {
					// 在 H5/App 端可使用 window.open(url) 或 uni.navigateTo({ url: `/pages/webview/webview?url=${encodeURIComponent(url)}` })
					console.log(`正在跳转到: ${url}`);
				}
			}
		});
	};

	onUnmounted(() => {
		clearInterval(countdownTimer);
	});
</script>

<style scoped>
	@keyframes pulse {

		0%,
		100% {
			transform: scale(1);
		}

		50% {
			transform: scale(1.05);
		}
	}

	/* Countdown 动画 */
	/* 这里我们将依赖 Vue 状态和 stroke-dashoffset 的计算，但为了模拟原 HTML 的纯 CSS 动画效果，
   需要将 stroke-dashoffset 初始值设置在 style 中，并让 animation 跑完 */
	@keyframes countdown-uni {
		to {
			stroke-dashoffset: 0;
		}
	}

	.countdown-circle {
		transform: rotate(-90deg);
		/* 保持与原 HTML 一致的旋转 */
		transform-origin: center;
		/* 初始状态由 Vue computed style 设置，动画 duration 由 Vue data 传入 */
		/* 实际运行中，UniApp 平台对 CSS 动画支持的差异，可能需要调整 keyframes 的定义或使用 Vue/JS 来控制 stroke-dashoffset */
		animation: countdown-uni linear forwards;
	}

	/* --- 基础样式 --- */
	.container {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background-color: #f9fafb;
	}

	.scroll-content {
		flex: 1;
		/* 确保滚动区域在底部留有空间 */
		padding-bottom: 30rpx;
	}

	/* --- 软件信息栏 --- */
	.app-info-bar {
		background-color: #ffffff;
		padding: 32rpx;
		border-bottom: 1rpx solid #f3f4f6;
	}

	.app-logo {
		width: 96rpx;
		height: 96rpx;
		background-color: #e0f2fe;
		border-radius: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-shrink: 0;
	}

	.logo-icon {
		color: #2563eb;
		font-size: 40rpx;
	}

	.app-name {
		font-weight: bold;
		color: #1f2937;
		display: block;
		font-size: 30rpx;
	}

	.app-specs {
		font-size: 28rpx;
		color: #6b7280;
		display: block;
	}

	.flex-row {
		display: flex;
		flex-direction: row;
	}

	.items-center {
		align-items: center;
	}

	.items-start {
		align-items: flex-start;
	}

	.space-x-3 {
		gap: 24rpx;
	}

	.space-x-1 {
		gap: 8rpx;
	}

	.space-y-3 {
		gap: 24rpx;
		display: flex;
		flex-direction: column;
	}

	/* --- 下载方式选择 --- */
	.section-container {
		padding: 32rpx;
	}

	.section-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #1f2937;
		margin-bottom: 32rpx;
		display: block;
	}

	.download-option {
		background-color: #ffffff;
		border-radius: 32rpx;
		padding: 32rpx;
		box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
		border: 2rpx solid transparent;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.download-option.selected {
		border-color: #3b82f6;
		border-width: 4rpx;
	}

	.option-icon-box {
		width: 80rpx;
		height: 80rpx;
		border-radius: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-shrink: 0;
	}

	.bg-blue {
		background-color: #dbeafe;
	}

	.bg-orange {
		background-color: #fff7ed;
	}

	.bg-green {
		background-color: #ecfdf5;
	}


	.option-icon-blue {
		color: #2563eb;
	}

	.option-icon-orange {
		color: #ea580c;
	}

	.option-icon-green {
		color: #059669;
	}

	.option-name {
		font-weight: bold;
		color: #1f2937;
		display: block;
		font-size: 30rpx;
	}

	.option-desc {
		font-size: 28rpx;
		color: #6b7280;
		display: block;
	}

	.select-check-circle {
		width: 40rpx;
		height: 40rpx;
		border-radius: 9999rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-shrink: 0;
	}

	.select-empty-circle {
		width: 40rpx;
		height: 40rpx;
		border: 4rpx solid #d1d5db;
		border-radius: 9999rpx;
		flex-shrink: 0;
	}

	/* --- 广告观看区域 --- */
	.ad-box {
		background-color: #ffffff;
		border-radius: 32rpx;
		padding: 48rpx;
		text-align: center;
	}

	.ad-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #1f2937;
		margin-bottom: 16rpx;
		display: block;
	}

	.ad-subtitle {
		font-size: 28rpx;
		color: #6b7280;
		margin-bottom: 48rpx;
		display: block;
	}

	.ad-player-area {
		background-color: #1f2937;
		border-radius: 24rpx;
		height: 384rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 48rpx;
		position: relative;
	}

	.ad-player-content {
		color: #ffffff;
		text-align: center;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.ad-playing .ad-play-icon {
		animation: none;
		/* 播放中停止脉冲 */
	}

	.ad-play-icon {
		font-size: 96rpx;
		margin-bottom: 32rpx;
	}

	.pulse {
		animation: pulse 2s infinite;
	}

	.ad-play-text {
		font-size: 32rpx;
		font-weight: 500;
	}

	/* 倒计时圆环 */
	.countdown-wrapper {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.countdown-svg-box {
		width: 96rpx;
		height: 96rpx;
		position: relative;
		display: block;
	}

	.countdown-svg {
		width: 100%;
		height: 100%;
	}

	.countdown-text-box {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.countdown-text {
		color: #ffffff;
		font-size: 28rpx;
		font-weight: bold;
	}

	.ad-meta-info {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		gap: 32rpx;
		font-size: 28rpx;
		color: #6b7280;
		margin-bottom: 48rpx;
	}

	.unlock-btn {
		width: 100%;
		background-color: #d1d5db;
		color: #6b7280;
		padding: 32rpx 0;
		border-radius: 24rpx;
		font-weight: bold;
		font-size: 36rpx;
		cursor: not-allowed;

		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		gap: 16rpx;
		line-height: 1;
		border: none;
	}

	.unlock-btn::after {
		border: none;
	}

	.unlock-btn[disabled] {
		opacity: 1;
	}

	.btn-icon {
		font-size: 32rpx;
	}

	/* --- 下载链接区域 (已解锁) --- */
	.download-links-box {
		background-color: #f0fdf4;
		border-radius: 32rpx;
		padding: 48rpx;
		border: 4rpx solid #dcfce7;
	}

	.success-message {
		text-align: center;
		margin-bottom: 32rpx;
	}

	.success-icon {
		color: #059669;
		font-size: 64rpx;
		margin-bottom: 16rpx;
		display: block;
	}

	.success-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #065f46;
		display: block;
	}

	.success-subtitle {
		font-size: 28rpx;
		color: #059669;
		display: block;
	}

	.link-item {
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 32rpx;
		margin-bottom: 32rpx;
	}

	.link-header {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 8rpx;
	}

	.link-label {
		font-size: 28rpx;
		font-weight: 500;
		color: #4b5563;
	}

	.link-copy-btn {
		color: #2563eb;
		font-size: 28rpx;
		padding: 8rpx 16rpx;
	}

	.link-content {
		font-size: 28rpx;
		color: #1f2937;
		background-color: #f9fafb;
		padding: 16rpx;
		border-radius: 8rpx;
		word-break: break-all;
		display: block;
	}

	.code-content {
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
		color: #1f2937;
		background-color: #f9fafb;
		padding: 24rpx;
		border-radius: 8rpx;
		letter-spacing: 12rpx;
		display: block;
	}

	.open-link-btn {
		width: 100%;
		background-color: #2563eb;
		color: #ffffff;
		padding: 32rpx 0;
		border-radius: 24rpx;
		font-weight: bold;
		font-size: 36rpx;

		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		gap: 16rpx;
		line-height: 1;
		border: none;
	}

	.open-link-btn::after {
		border: none;
	}


	/* --- 下载说明 --- */
	.instruction-box {
		background-color: #eff6ff;
		border-radius: 32rpx;
		padding: 32rpx;
	}

	.instruction-icon {
		color: #2563eb;
		margin-top: 8rpx;
		font-size: 36rpx;
		flex-shrink: 0;
	}

	.instruction-title {
		font-weight: bold;
		color: #1e40af;
		margin-bottom: 16rpx;
		display: block;
		font-size: 30rpx;
	}

	.instruction-list {
		display: flex;
		flex-direction: column;
		gap: 8rpx;
		font-size: 28rpx;
	}

	.instruction-list-item {
		color: #1d4ed8;
		display: block;
		line-height: 1.5;
	}


	/* --- 底部安全提示 --- */
	.bottom-security-tip {
		padding: 32rpx;
	}

	.bg-gray-tip {
		background-color: #f3f4f6;
		border-radius: 24rpx;
		padding: 32rpx;
		text-align: center;
	}

	.security-icon {
		color: #4b5563;
		font-size: 40rpx;
		margin-bottom: 16rpx;
		display: block;
	}

	.security-text {
		font-size: 28rpx;
		color: #4b5563;
		display: block;
	}
</style>